package lovemusic.lanou.com.newsmusic.ui.fragment;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import lovemusic.lanou.com.newsmusic.R;
import lovemusic.lanou.com.newsmusic.models.MusicInfo;
import lovemusic.lanou.com.newsmusic.service.OnPlayerEventListener;
import lovemusic.lanou.com.newsmusic.service.PlayMusicService;
import lovemusic.lanou.com.newsmusic.ui.activity.MainActivity;
import lovemusic.lanou.com.newsmusic.ui.adapter.Music_viewpager_adapter;
import lovemusic.lanou.com.newsmusic.utils.CoverLoader;
import lovemusic.lanou.com.newsmusic.utils.SystemUtils;
import lovemusic.lanou.com.newsmusic.utils.UIUtils;

/**
 * 音乐界面的fragment
 * Created by lanouhn on 2016/9/28.
 */
public class Fragment_music extends Fragment implements View.OnClickListener,OnPlayerEventListener{
    private ViewPager music_viewpager;
    private Music_viewpager_adapter mAdapter;
    private List<Fragment> fragments;
    private Fragment_localmusic ft_localmusic;
    private Fragment_onlinemusic ft_onlinemusic;
    public  PlayMusicService mPlayService;
    private TextView tv_load_music;
    private TextView tv_online_music;
    private ImageView iv_play_bar_cover;
    private TextView tv_title_bar;
    private TextView tv_play_bar_artist;
    private ImageView iv_paly_bar_play;
    private ImageView ic_paly_bar_next;
    private ProgressBar pb_play_bar;
    private FrameLayout fl_play_bar;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return View.inflate(getActivity(),R.layout.fragent_music,null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        bindService();
    }

    /**
     * 绑定服务对象
     */
    private void bindService() {
        Intent intent = new Intent();
        intent.setClass(UIUtils.getContext(), PlayMusicService.class);
        getActivity().bindService(intent,mPlayServiceConnection,Context.BIND_AUTO_CREATE);
    }


    public ServiceConnection mPlayServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mPlayService = ((PlayMusicService.PlayBinder) service).getService();
            mPlayService.setOnPlayEventListener(Fragment_music.this);
            init();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    private void init() {

        initview();
        initEvent();
    }

    private void initview() {
        tv_load_music = (TextView) getActivity().findViewById(R.id.tv_load_music);
        tv_online_music = (TextView)getActivity().findViewById(R.id.tv_online_music);
        music_viewpager= (ViewPager) getView().findViewById(R.id.music_viewpager);
        iv_play_bar_cover= (ImageView) getView().findViewById(R.id.iv_play_bar_cover);
        iv_paly_bar_play= (ImageView) getView().findViewById(R.id.iv_paly_bar_play);
        ic_paly_bar_next= (ImageView) getView().findViewById(R.id.ic_paly_bar_next);
        pb_play_bar= (ProgressBar) getView().findViewById(R.id.pb_play_bar);
        fl_play_bar= (FrameLayout) getView().findViewById(R.id.fl_play_bar);
        tv_title_bar= (TextView) getView().findViewById(R.id.tv_title_bar);
        tv_play_bar_artist= (TextView) getView().findViewById(R.id.tv_play_bar_artist);
        ft_localmusic=new Fragment_localmusic();
        ft_onlinemusic=new Fragment_onlinemusic();
        fragments=new ArrayList<Fragment>();
        fragments.add(ft_localmusic);
        fragments.add(ft_onlinemusic);
        mAdapter=new Music_viewpager_adapter(getChildFragmentManager(),fragments);
        music_viewpager.setAdapter(mAdapter);
        music_viewpager.setCurrentItem(0);
        setlistener();

    }

    private void setlistener() {
        tv_load_music.setOnClickListener(this);
        tv_online_music.setOnClickListener(this);
        iv_paly_bar_play.setOnClickListener(this);
        ic_paly_bar_next.setOnClickListener(this);
        fl_play_bar.setOnClickListener(this);
    }

    private void initEvent() {
//        给viewpage设置监听事件来同步toolbar上的标题
        music_viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        tv_load_music.setSelected(true);
                        tv_online_music.setSelected(false);
                        break;
                    case 1:
                        tv_online_music.setSelected(true);
                        tv_load_music.setSelected(false);
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });


    }
    /**
     * 提供获得Service对象的方法
     *
     * @return
     */
    public  PlayMusicService getPlayService() {
        return mPlayService;
    }

    /**
     * 播放或者暂停
     */
    private void play() {
        getPlayService().playPause();
    }

    /**
     * 实现下一曲
     */
    private void next() {
        getPlayService().next();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_load_music:
                tv_load_music.setSelected(true);
                tv_online_music.setSelected(false);
                music_viewpager.setCurrentItem(0);
                break;
            case R.id.tv_online_music:
                tv_online_music.setSelected(true);
                tv_load_music.setSelected(false);
                music_viewpager.setCurrentItem(1);
                break;
            case R.id.iv_paly_bar_play:
                play();
                break;
            case R.id.ic_paly_bar_next:
                next();
                break;
            case R.id.fl_play_bar:
//                showPlayingFragment();
                break;


        }
    }

    public void onPlay(MusicInfo musicInfo) {
        if (musicInfo == null) {
            return;

        }
        Bitmap cover;
        if (musicInfo.getUri() != null) {
            cover = CoverLoader.getInstance().loadThumbnail(musicInfo.getCoverUri());
        } else {
            cover = musicInfo.getCover();
        }
        iv_play_bar_cover.setImageBitmap(cover);
        tv_title_bar.setText(musicInfo.getTitle());
        tv_play_bar_artist.setText(musicInfo.getArtist());
        if (getPlayService().isPlaying()) {
            iv_paly_bar_play.setSelected(true);
        } else {
            iv_paly_bar_play.setSelected(false);
        }

//        将音乐的时间长度设置为进度条的最大值
        pb_play_bar.setMax((int) musicInfo.getDuration());
        pb_play_bar.setProgress(0);
        if (ft_localmusic != null) {
            ft_localmusic.onItemPlay();
        }


    }


    @Override
    public void onPublish(int progress) {
        pb_play_bar.setProgress(progress);
    }

    @Override
    public void onChange(MusicInfo music) {
        onPlay(music);

    }

    @Override
    public void onPlayerPause() {

        iv_paly_bar_play.setSelected(false);
    }

    @Override
    public void onPlayerResume() {
        iv_paly_bar_play.setSelected(true);

    }

    @Override
    public void onTimer(long remain) {
//        if (timerItem==null){
//            timerItem= navigationView.getMenu().findItem(R.id.menu_stop_play);
//        }
//        String titile="定时停止播放 ";
//        timerItem.setTitle(remain==0?titile: SystemUtils.formatTime(titile+("mm:ss"),remain));


    }
}
